package com.ruoyi.system.controller;

import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.system.domain.Resume;
import com.ruoyi.system.service.IResumeService;
import com.ruoyi.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Recruitment;
import com.ruoyi.system.service.IRecruitmentService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 招聘管理Controller
 *
 * @author ruoyi
 * @date 2022-11-01
 */
@Api("招聘管理")
@Controller
@RequestMapping("/system/recruitment")
public class RecruitmentController extends BaseController
{
    private String prefix = "system/recruitment";

    @Resource
    private IRecruitmentService recruitmentService;

    @Resource
    private ISysUserService userService;

    @Resource
    private IResumeService resumeService;

    @ApiOperation("跳转招聘管理列表页")
    @RequiresPermissions("system:recruitment:view")
    @GetMapping()
    public String recruitment(ModelMap mmap)
    {
        Long roleId = ShiroUtils.getSysUser().getRoles().get(0).getRoleId();
        mmap.put("roleId", roleId);
        return prefix + "/recruitment";
    }

    /**
     * 查询招聘管理列表
     * @param recruitment 招聘管理对象
     * @return
     */
    @RequiresPermissions("system:recruitment:list")
    @ApiOperation("获取招聘信息列表")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Recruitment recruitment)
    {
        Long roleId = ShiroUtils.getSysUser().getRoles().get(0).getRoleId();
        //如果是招聘者
        if (ObjectUtils.isNotEmpty(roleId) && roleId == 3) {
            recruitment.setRecruitmentCompany(ShiroUtils.getSysUser().getUserName());
        }
        startPage();
        List<Recruitment> list = recruitmentService.selectRecruitmentList(recruitment);
        return getDataTable(list);
    }

    /**
     * 导出招聘管理列表
     * @param recruitment 招聘管理对象
     * @return
     */
    @RequiresPermissions("system:recruitment:export")
    @Log(title = "招聘管理", businessType = BusinessType.EXPORT)
    @ApiOperation("招聘信息导出")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Recruitment recruitment)
    {
        List<Recruitment> list = recruitmentService.selectRecruitmentList(recruitment);
        ExcelUtil<Recruitment> util = new ExcelUtil<Recruitment>(Recruitment.class);
        return util.exportExcel(list, "招聘管理数据");
    }

    /**
     * 新增招聘管理
     */
    @ApiOperation("跳转招聘信息新增页面")
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存招聘管理
     * @param recruitment 招聘管理对象
     */
    @RequiresPermissions("system:recruitment:add")
    @Log(title = "招聘管理", businessType = BusinessType.INSERT)
    @ApiOperation("招聘信息新增")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Recruitment recruitment)
    {
        return toAjax(recruitmentService.insertRecruitment(recruitment));
    }

    /**
     * 修改招聘管理
     * @param recruitmentId 招聘管理信息id
     * @param mmap 返回到页面的信息集合
     * @return
     */
    @ApiOperation("跳转招聘信息编辑页面")
    @RequiresPermissions("system:recruitment:edit")
    @GetMapping("/edit/{recruitmentId}")
    public String edit(@PathVariable("recruitmentId") Long recruitmentId, ModelMap mmap)
    {
        Recruitment recruitment = recruitmentService.selectRecruitmentByRecruitmentId(recruitmentId);
        mmap.put("recruitment", recruitment);
        return prefix + "/edit";
    }

    /**
     * 修改保存招聘管理
     * @param recruitment 招聘管理对象
     * @return
     */
    @RequiresPermissions("system:recruitment:edit")
    @Log(title = "招聘管理", businessType = BusinessType.UPDATE)
    @ApiOperation("招聘信息修改")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Recruitment recruitment)
    {
        return toAjax(recruitmentService.updateRecruitment(recruitment));
    }

    /**
     * 删除招聘管理
     * @param ids 待删除的招聘管理数据id集合
     * @return
     */
    @RequiresPermissions("system:recruitment:remove")
    @Log(title = "招聘管理", businessType = BusinessType.DELETE)
    @ApiOperation("招聘信息删除")
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(recruitmentService.deleteRecruitmentByRecruitmentIds(ids));
    }

    /**
     * 跳转详招聘信息详情页
     * */
    @ApiOperation("跳转详招聘信息详情页")
    @Log(title = "跳转详招聘信息详情页", businessType = BusinessType.UPDATE)
    @GetMapping("/choseResume/{recruitmentId}")
    public String choseResume(@PathVariable("recruitmentId") Long recruitmentId, ModelMap mmap) {
        // 页面filter条件
        mmap.put("recruitmentId", recruitmentId);
        return prefix + "/choseResume";
    }

    /**
     * 跳转详招聘信息详情页
     * */
    @ApiOperation("跳转详招聘信息详情页")
    @Log(title = "跳转详招聘信息详情页", businessType = BusinessType.UPDATE)
    @GetMapping("/detail/{recruitmentId}")
    public String detail(@PathVariable("recruitmentId") Long recruitmentId, ModelMap mmap) {
        // 页面filter条件
        mmap.put("recruitment", recruitmentService.selectRecruitmentByRecruitmentId(Long.valueOf(recruitmentId)));
        return prefix + "/detail";
    }

    /**
     * 获取用户所有简历
     * @return
     */
    @ApiOperation("获取用户所有简历")
    @PostMapping("/getAllResume")
    @ResponseBody
    public AjaxResult getAllResume()
    {
        String userName = ShiroUtils.getSysUser().getUserName();
        Resume resume = new Resume();
        resume.setResumeName(userName);
        List<Resume> resumes = resumeService.selectResumeList(resume);
        List<Long> resumeIds = resumes.stream().map(item -> item.getResumeId()).collect(Collectors.toList());
        return AjaxResult.success(resumeIds);
    }

    /**
     * 发送简历
     * @param recruitmentId 简历id
     * @return
     */
    @ApiOperation("发送简历")
    @PostMapping("/giveRecruitment")
    @ResponseBody
    public AjaxResult giveRecruitment(String recruitmentId, String resumeId)
    {
        return AjaxResult.success(recruitmentService.giveRecruitment(recruitmentId, resumeId));
    }

}
